<?php
namespace modules\user;
use modules\auth\security;

class storage
{
    static function schemas(): array
    {
        return [
            'users' => [
                'id' => 'identity',
                'username' => 'string/64',
                'credential' => 'string',
                'org' => '@orgs/org',
                'name' => 'string/64',
                'role' => '*role/admin:salesman',
                'comment' => 'string',
                'seq' => 'string',
                'active' => 'bool',
            ],
        ];
    }

    static function add($q,
        string $username, string $password, string $role,
        string $name, string $comment, int $org)
    {
        return $q->table('users')
            ->field('username',
                    'credential',
                    'role',
                    'seq',
                    'name',
                    'comment',
                    'org',
                    'active')
            ->insert($username,
                     security::make_credential($password),
                     $role,
                     security::random_id(),
                     $name,
                     $comment,
                     $org,
                     true);
    }

    static function reset_password($q, $org, $id, $password)
        // will revoke the token BTW.
    {
        return $q->table('users', $id)
            ->field('credential', 'seq')
            ->where(['org', $org])
            ->update(security::make_credential($password),
                     security::random_id());
    }

    static function reset_role($q, $org, $id, $role)
        // will revoke the token BTW.
    {
        return $q->table('users', $id)
            ->field('seq', 'role')
            ->where(['org', $org])
            ->update(security::random_id(), $role);
    }

    static function profile($q, int $org, int $id)
    {
        return $q->table('users', $id)
            ->where(['org', $org])
            ->field('id',
                    'name',
                    'username',
                    'role',
                    'org.name orgname',
                    'comment')
            ->select(0);
    }

    static function id_from_username($q, int $org, string $username)
    {
        return $q->table('users')
            ->where(['org', $org])
            ->where(['username', $username])
            ->field('id')
            ->select(0);
    }

    static function list_by_active($q, int $org, int $page, bool $active)
    {
        return $q->table('users')
            ->where(['org', $org])
            ->where(['active', $active])
            ->field('id', 'username', 'name', 'role')
            ->select(10, $page);
    }

    static function active($q, int $org, int $id, bool $active)
    {
        return $q->table('users', $id)
            ->where(['org', $org])
            ->field('active')
            ->update($active);
    }

    static function others($q, int $org, int $id, array $data)
    {
        return $q->table('users', $id)
            ->where(['org', $org])
            ->field(...array_keys($data))
            ->update(...array_values($data));
    }
}

